<?php

require_once('./modules/feeds/feed.inc');

//Feed editor page.
class EditFeedRenderer extends Renderer
{
	protected $_feed;
	
	function pageLoad()
	{
		switch( $this->action )
		{
			case "save":
				$this->loadFeed();
				$this->saveFeed();
				break;
			case "":
			case "show":
			default:
				$this->loadFeed();
				break;
		}
	}

	function isPermitted()
	{
		//echo "permission check feeds";
		if ( isset($this->user) && $this->user->type == 2 )
			return true;
		else
			return false;
	}
	
	function loadFeed()
	{
		$this->_feed = new Feed();
		
		$this->_feed->load($_GET['feedId']);
	}
	
	function saveFeed()
	{
		$feed = 
		$desc = $_POST['txtDesc'];

		$this->_feed->feedName = $_POST['txtFeedName'];
		$this->_feed->description = $_POST['txtDesc'];
		$this->_feed->url = $_POST['txtUrl'];

		$result = $this->_feed->update();
				
		if($result > 0)
			header("Location: index.php?pg=edit_feed&status=success&feedId=".$this->_feed->id);
		else
			header("Location: index.php?pg=edit_feed&status=failed&feedId=".$this->_feed->id);

	}
	
	function renderContent()
	{
		global $content;
		global $siteName;
		
		$content = 
		"<div class='headerPitch'>
			<p><h1>Edit feed ".$this->_feed->feedName."</h1></p>
		</div>
		<div class='pageContentTitle'>
			<p></p>
		</div>
		<div class='pageContent'>
			<p>
			<form method='POST' action='index.php?pg=edit_feed&act=save&feedId=".$this->_feed->id."'>
			<fieldset style='width:440px;padding:20px;'>
				<legend>Feed details:</legend>
				name: <input type='text' style='margin-bottom:10px;margin-top:3px;' name='txtFeedName' id='txtFeedName' size='80' value='".$this->_feed->feedName."'></input><br />
				description: <input type='text' style='margin-bottom:10px;margin-top:3px;' name='txtDesc' id='txtDesc' size='80' value='".$this->_feed->description."'></input><br />
				url: <input type='text' style='margin-bottom:10px;margin-top:3px;' name='txtUrl' id='txtUrl' size='80' value='".$this->_feed->url."'></input><br /><br/>
				<input type='submit' value='Save'/>";
		if( isset($GET_['status']) && $_GET['status'] == 'failed' )
			$content .= "<P/><label style='color:red;'>Failed to save feed</label>";
		else if(isset($_GET['status']) && $_GET['status'] == "success")
			$content .= "<P/><label style='color:green;'>Feed saved successfully</label>";
		
		$content .= "				</fieldset>
			</form>
			</p>
		</div>";
	}
}

$renderer = new EditFeedRenderer();

?>
